<template>
	<view class="main_style">
		<view style="flex: 1;justify-content: center;align-items: center;font-size: 22rpx;color: #eee;display: flex;" v-if="errorModel.code == -1" @click="refresh">加载失败,点击重新加载</view>
	</view>
</template>


<script>
	var _this = null;
	export default {
		data() {
			return {
				errorModel:null
			}
		},
		onLoad() {
			console.log('onload');
			_this = this;
			this.fetchMovice();
		},
		methods: {
			refresh(){
				this.errorModel = null;
				this.fetchMovice();
			},
			login() {
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						uni.showLoading();
						let data = {};
						data.js_code = loginRes.code;
						// console.log(_this.params.params.user_pid);
						if (_this.params && _this.params.params && _this.params.params.bm_user_pid) {
							data.user_pid = _this.params.params.bm_user_pid;
						}
						_this.$request.post({
							url: "/mUser/login",
							data: data,
							success(res) {
								uni.hideLoading();
								if (res) {
									uni.setStorageSync('token', res.userInfo.user_token);
									uni.setStorageSync('userInfo', res.userInfo);
									_this.dispatch();
								}
							},
							fail:(failed)=> {
								this.errorModel = failed;
								uni.hideLoading();
								console.log(failed);
							}
						});
					}
				});
			},
			fetchMovice() {
				uni.showLoading();
				this.$request.post({
					url: "/mUser/userInfo",
					reload: 'reload',
					success(res) {
						uni.hideLoading();
						console.log(res);
						if (res != null) {
							uni.setStorageSync('userInfo', res.userInfo);
						}
						_this.dispatch();
					},
					fail:(failed)=> {
						if (failed.code == 401) {
							_this.login();
						}else{
							this.errorModel = failed;
						}
						uni.hideLoading();
					}
				});
			},
			dispatch(){
				uni.switchTab({
					url: '/pages/wm/main'
				});
			}
		}
	}
</script>

<style lang="stylus" scoped>
	
	.main_bg{
		background-color #eee;
	}
	
	.item_class{
		display flex;
		margin-bottom 20rpx;
		margin-left 20rpx;
		margin-right 20rpx;
		height 130rpx;
		background-color #fff;
		border-radius 10rpx;
		justify-content space-between;
		align-items center;
		
		.left_class{
			margin-left 33rpx;
			width 64rpx;
			height 64rpx;
			background-color rgba(0, 0, 0, 0);
			font-size 30rpx;
			font-weight bold;
		}
		
		.middle_class{
			display flex;
			flex-direction column;
			margin-left 20rpx;
			text-align left;
			flex 1;
			
			.middle_top {
				font-size 28rpx;
				font-weight bold;
				line-height 40rpx;
				height 40rpx;
			}
			
			.middle_bottom {
				color #999999;
				font-size 22rpx;
				line-height 30rpx;
				height 30rpx;
			}
		}
		
	}
	
</style>
